<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    /**
    * Main class for working with site pages named "posts".
    * @author NU SIT Bar
    * @version 1.0
    * @created 17.Jul.2012 17:39:44 h
    */

    class Posts extends MX_Controller 
    {

        private $data;

        function __construct()
        {

            parent::__construct();
            if(!$this->session->userdata('logged_in')){
                redirect(base_url());    
            }
            $this->load->config('pages');

        }

        public function index()
        { 
            if(!isset($_GET['post_type']))
            {
                $_GET['post_type']="page";
            }
            $this->view_all();
        }

        /**
        * 
        * @param $_GET['post_type']
        */
        function view_all()
        {   
            $this->data['page'] = ucfirst($_GET['post_type']);
            $this->data['title'] = 'View all '.$_GET['post_type'];
            $this->template->build('posts/view_all',$this->data);
        }

        function view_new()
        {  
            $this->data['post_action'] = "NEW"; 
            $this->data['page'] = ucfirst($_GET['post_type']);
            $this->data['title'] = 'Add '.$_GET['post_type'];
            $this->template->build('posts/edit_post', $this->data);        
        }

        /**
        * 
        * @param $_GET['post_ID']
        * @param $_GET['post_type']
        */
        function view_edit()
        {      
            $this->data['post_action'] = "EDIT";          
            $this->data['page'] = ucfirst($_GET['post_type']);
            $this->data['title'] = 'View all '.$_GET['post_type'];
            $this->template->build('posts/edit_post', $this->data);
        }

        /**
        * 
        * @param $_POST['parent_id']
        * @param $_POST['post_type']
        * @param $_POST['post_title']
        * @param $_POST['post_action']
        * @param $_POST['content']
        * @param $_POST['qtrans_textarea_content']
        */
        function submit_post(){

            /** module run page submit_post **/
            $result = modules::run('pages/submit_post',array('link'=>'posts'));

            $result = explode('-',$result);

            // Return
            if($result[0]){
                echo json_encode(array('action'=>true,'msg'=>$result[1]));
            }else{
                echo json_encode(array('action'=>true,'msg'=>'Something went wrong. Please try again.'));
            }

        }

        function delete($ID){

            /** module run page submit_post **/
            $result = modules::run('pages/delete',array('ID'=>$ID));

            if($result){
                $this->view_all();
            }else{
                show_error('Error ocured while deleting page!');
            }

        }


    }

    /* End of file pages.php */
    /* Location: ./application/modules/pages/controllers/pages.php */